package controller;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.mybatis.po.Courseinfo;

import service.CourseService;
import util.MySessionContext;

@CrossOrigin(originPatterns="*",allowCredentials="true")
@Controller
@RequestMapping("course")
@EnableWebMvc
public class Course {
	@Autowired
	private CourseService courseservice;
	private ServletContext servletContext;
	@PostMapping("add")
	@ResponseBody
	public int add(@RequestBody Courseinfo courseinfo) {
		String ans="";
		int cid=0;
		 MySessionContext myc= MySessionContext.getInstance();
		 HttpSession session = myc.getSession(courseinfo.getSid());
		 courseinfo.setTname((String)session.getAttribute("Uname"));
		try{
			cid=courseservice.addcourse(courseinfo);
			ans+="添加成功";
		}catch (Exception e){
			ans+="数据已存在";
		}
		return cid;
	}
	@PostMapping("delete")
	@ResponseBody
	public String delete(String cid) {
		String ans="";
		if(courseservice.deletecourse(cid)==0) {
			ans+="数据不存在";
		}
		else ans+="删除成功";
		return ans;
	}
	@PostMapping("update")
	@ResponseBody
	public String update(@RequestBody Courseinfo courseinfo) {
		String ans="";
		if(courseservice.updatecourse(courseinfo)==0) {
			ans+="数据不存在";
		}
		else ans+="更新成功";
		return ans;
	}
	@PostMapping("selectall")
	@ResponseBody
	public List<Map<String,Object>> selectall() {
		return courseservice.selectAllCourse();
	}
	@RequestMapping("addvedio")
	@ResponseBody
	public String addvedio(List<MultipartFile> vedio,String cid) {
		return courseservice.addvedio(vedio,cid);
	}
	@RequestMapping("image-resource")
	@ResponseBody
	public void execute(HttpServletResponse response) throws IOException {
		// 指定图片文件路径
		String filePath = "D:\\java\\eclipse-workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\sct\\upload\\d10bf4d1e104319ed55c61a0853cee25.mp4";

		// 创建File对象
		File file = new File(filePath);

		// 创建FileInputStream对象
		FileInputStream fis = new FileInputStream(file);
		// 创建BufferedInputStream对象
		BufferedInputStream bis = new BufferedInputStream(fis);

		// 创建ByteArrayOutputStream对象
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		// 定义缓冲区大小
		byte[] buffer = new byte[1024];
		int len;

		// 读取图片文件并写入缓冲区
		while ((len = bis.read(buffer)) > -1) {
		    baos.write(buffer, 0, len);
		}

		// 刷新缓冲区
		baos.flush();

		// 获取转换后的二进制数据
		byte[] imageData = baos.toByteArray();
		
		// 设置响应头信息
		response.setContentType("video/mp4");
		response.setContentLength(imageData.length);

		// 获取输出流
		ServletOutputStream sos = response.getOutputStream();

		// 将二进制数据写入输出流
		sos.write(imageData);
		sos.flush();
		sos.close();
	}
	@RequestMapping("image-resource1")
	@ResponseBody
	public String toBase64(String fileAbsolutePath) {
        FileInputStream inputStream = null;
        String base64Str = "";
        try {
            Base64.Encoder encoder = Base64.getEncoder();
            inputStream = new FileInputStream(fileAbsolutePath);
            int available = inputStream.available();
            byte[] bytes = new byte[available];
            inputStream.read(bytes);
            base64Str = encoder.encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return base64Str;
    }
//	public Resource getImageAsResource(HttpServletRequest request) {
//	   return new ServletContextResource(request.getServletContext(),"D:\\java\\eclipse-workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\sct\\upload\\5.jpg");
//	}
}
